﻿/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Material 2.0
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.12
import QtGraphicalEffects 1.0
import "qrc:/MaterialUI/"
import "qrc:/MaterialUI/Interface/"
Item {
    id: userconfig

    property string selectimage: "user_default"

    function initOpen(){
        selectimage=ChatManage.MyInfo["user_img"]
        imageView.visible = false
        imagemodel.clear()
        imagemodel.append({"image_name":"user_default"})
        for(var i=1;i<27;++i){
            imagemodel.append({"image_name":"user_"+i})
        }
    }

    function saveNowConfig(){
        if(ChatManage.Registe){
//            console.log(user_name.text,user_mail.text,user_pwd1.text,user_pwd2.text)
//            console.log(selectimage)
            if(user_pwd1.text.length>0||user_pwd2.text.length>0){
                if(user_pwd1.text!==user_pwd2.text){
                    materialUI.showSnackbarMessage("两次输入密码不一致，请重新输入")
                    return
                }
                if(user_pwd1.text.length<3){
                    materialUI.showSnackbarMessage("密码长度太短，请重新输入")
                    return
                }
            }
            if(user_name.text.length<1){
                materialUI.showSnackbarMessage("用户名不能为空")
                return
            }
            ChatManage.setUserConfig(user_name.text,user_pwd1.text,selectimage)
        }
        else
            materialUI.showSnackbarMessage("请检查网络连接正确后再进行参数设置！")
    }

    ListModel{
        id:imagemodel
    }

    MouseArea{
        anchors.fill: parent
        onWheel: {

        }
    }

    Rectangle{
        id: image
        width: 90
        height: 90
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.leftMargin: 20
        anchors.topMargin: 20
        Image {
            anchors.fill: parent
            anchors.margins: 5
            source: "qrc:/"+selectimage+".png"
            fillMode: Image.PreserveAspectFit
            layer.enabled: true
            layer.effect:DropShadow {
                horizontalOffset: 0
                verticalOffset: 0
                radius: 4
                samples: 9
                color: "#000000"
                spread:0
            }
        }
        layer.enabled: true
        layer.effect: DropShadow {
            horizontalOffset: 0
            verticalOffset: 0
            radius: 4
            samples: 9
            color: "#000000"
            spread: 0
        }
        Rectangle{
            id:showTip
            anchors.fill: parent
            visible: false
            opacity:0.5
            MaterialLabel{
                font.pixelSize: 15
                text: imageView.visible?"返 回":"更换头像"
                anchors.fill: parent
                horizontalAlignment: Label.AlignHCenter
                verticalAlignment: Label.AlignVCenter
            }
        }
        MouseArea{
            anchors.fill: parent
            hoverEnabled: enabled
            onEntered: {
                showTip.visible = true
            }
            onExited: {
                showTip.visible = false
            }
            onClicked: {
                //console.log("image is clicked")
                imageView.visible = !imageView.visible
            }
        }
    }


    ColumnLayout {
        anchors{
            left:image.right
            top:image.top
            bottom: parent.bottom
            right: parent.right
            leftMargin: 20
            rightMargin: 20
            bottomMargin: 20
        }

            Row {
                spacing: 12
                MaterialLabel{
                    font.pixelSize: 15
                    width:80
                    height: 50
                    text: "姓 名:"
                    horizontalAlignment: Label.AlignRight
                    verticalAlignment: Label.AlignVCenter
                }
                TextField {
                    id:user_name
                    wrapMode: TextArea.Wrap
                    selectByMouse: true
                    font.pixelSize: 18
                    width: 220
                    height: 50
                    text: ChatManage.MyInfo["user_name"]
                    Layout.fillWidth: true
                    maximumLength:12
                }
            }
            Row {
                spacing: 12
                MaterialLabel{
                    font.pixelSize: 15
                    width:80
                    height: 50
                    text: "邮 箱:"
                    horizontalAlignment: Label.AlignRight
                    verticalAlignment: Label.AlignVCenter
                }
                TextField {
                    id:user_mail
                    wrapMode: TextArea.Wrap
                    selectByMouse: true
                    font.pixelSize: 18
                    width: 220
                    height: 50
                    enabled: false
                    text: ChatManage.MyInfo["user_mail"]
                    Layout.fillWidth: true
                }
            }
            Row {
                spacing: 12
                MaterialLabel{
                    font.pixelSize: 15
                    width:80
                    height: 50
                    text: "新密码:"
                    horizontalAlignment: Label.AlignRight
                    verticalAlignment: Label.AlignVCenter
                }
                TextField {
                    id:user_pwd1
                    wrapMode: TextArea.Wrap
                    selectByMouse: true
                    font.pixelSize: 18
                    width: 220
                    height: 50
//                    text: value
                    Layout.fillWidth: true
                    maximumLength:12
                    passwordMaskDelay: 100
                    echoMode:TextField.Password
                }
            }
            Row {
                spacing: 12
                MaterialLabel{
                    font.pixelSize: 15
                    width:80
                    height: 50
                    text: "再次输入:"
                    horizontalAlignment: Label.AlignRight
                    verticalAlignment: Label.AlignVCenter
                }
                TextField {
                    id:user_pwd2
                    wrapMode: TextArea.Wrap
                    selectByMouse: true
                    font.pixelSize: 18
                    width: 220
                    height: 50
//                    text: value
                    Layout.fillWidth: true
                    maximumLength:12
                    passwordMaskDelay: 100
                    echoMode:TextField.Password
                }
            }

    }
    Rectangle{
        id:imageView
        visible: false
        anchors{
            left:image.right
            top:image.top
            bottom: parent.bottom
            right: parent.right
            leftMargin: 20
            rightMargin: 10
            bottomMargin: 10
        }
        Component {
            id: contactDelegate
            Item {
                width: grid.cellWidth; height: grid.cellHeight
                Column {
                    anchors.fill: parent
                    Rectangle{
                        id:baccolor
                        width: 85
                        height: 85
                        radius: 3
                        color: "#00000000"
                        Image {
                            id:imageItem
                            source: "qrc:/"+image_name+".png"
                            anchors.fill: parent
                            anchors.margins: 5
                            anchors.horizontalCenter: parent.horizontalCenter
                            fillMode: Image.PreserveAspectFit
                            layer.enabled: true
                            layer.effect:DropShadow {
                                horizontalOffset: 0
                                verticalOffset: 0
                                radius: 4
                                samples: 9
                                color: "#000000"
                                spread:0
                            }
                            PropertyAnimation {
                                id: animation
                                target: imageItem
                                property: "scale"
                                to: 1;
                                duration: 150
                            }
                            MouseArea{
                                anchors.fill: parent
                                hoverEnabled: true
                                onEntered: {
                                    baccolor.color = "lightskyblue"
                                    imageItem.scale = 0.9
                                    animation.running = true
                                }
                                onExited: {
                                    baccolor.color = "#00000000"
                                }

                                onClicked: {
                                    selectimage = image_name
                                    imageView.visible=false
                                }
                            }
                        }
                    }
                }
            }
        }
        GridView{
            id:grid
            anchors.fill: parent
            model: imagemodel
            cellWidth:100
            cellHeight:100
            //            highlight: Rectangle { color: "lightsteelblue"; radius: 1 }
            focus: true
            clip: true
            delegate:contactDelegate
            ScrollBar.vertical: ScrollBar {}
        }
    }
}

/*##^##
Designer {
    D{i:0;autoSize:true;height:480;width:640}
}
##^##*/
